package com.fsm.common.easyexcel.util;

import com.alibaba.excel.EasyExcel;
import com.fsm.common.tools.util.DateUtils;
import lombok.Data;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 导入表格，返回数组
 */
@Data
public class ExcelUtils {

    public static List<List<String>> parseFromExcel(InputStream fis) throws Exception{
        List<List<String>> result = new ArrayList<>();
        Workbook workbook = WorkbookFactory.create(fis);
        int sheetNumber =  workbook.getNumberOfSheets();
        if(sheetNumber == 1){
            Sheet sheet = workbook.getSheetAt(0);
            for(int rowNo = 5;rowNo < sheet.getLastRowNum() + 1;rowNo ++ ){
                Row row = sheet.getRow(rowNo);
                int startNumber = row.getFirstCellNum();
                int endNumber = row.getLastCellNum();
                List<String> strMessage = new ArrayList<>();
                for(int i = startNumber;i < endNumber;i++ ){
                    if(i == 18){
                        System.out.println(row.getCell(i));
                    }
                    System.out.println(row.getCell(i).getCellTypeEnum());
                    if(row.getCell(i).getCellTypeEnum().compareTo(CellType._NONE) == 0){
                        strMessage.add(row.getCell(i).getRichStringCellValue() + "");
                    }
                    if(row.getCell(i).getCellTypeEnum().compareTo(CellType.STRING) == 0){
                        strMessage.add(row.getCell(i).getStringCellValue());
                    }
                    if(row.getCell(i).getCellTypeEnum().compareTo(CellType.NUMERIC) == 0){
                        if(i == 6 || i == 10 || i == 11 || i == 12){
                            strMessage.add(DateUtils.dateTurnString(row.getCell(i).getDateCellValue(),"yyyy-MM-dd HH:mm:ss"));
                        }else if(i == 18){
                            String str = row.getCell(i).getNumericCellValue() + "";
                            strMessage.add(str.substring(0, str.indexOf("E")).replace(".",""));
                        }else{
                            strMessage.add(row.getCell(i).getNumericCellValue() + "");
                        }
                    }
                    if(row.getCell(i).getCellTypeEnum().compareTo(CellType.FORMULA) == 0){
                        strMessage.add(row.getCell(i).getCellFormula());
                    }
                    if(row.getCell(i).getCellTypeEnum().compareTo(CellType.BLANK) == 0){
                        strMessage.add(row.getCell(i).getStringCellValue());
                    }
                    if(row.getCell(i).getCellTypeEnum().compareTo(CellType.BOOLEAN) == 0){
                        strMessage.add(row.getCell(i).getBooleanCellValue() + "");
                    }
                    if(row.getCell(i).getCellTypeEnum().compareTo(CellType.ERROR) == 0){
                        strMessage.add(row.getCell(i).getErrorCellValue() + "");
                    }
                }
                result.add(strMessage);
            }
        }
        return result;
    }

}
